package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallVoucherOrderMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LitemallOrderVoucherService {
    @Resource
    private LitemallVoucherOrderMapper voucherOrderMapper;

    public List<LitemallVoucherOrder> querySelective(Integer userId, Integer page, Integer size) {
        LitemallVoucherOrderExample example = new LitemallVoucherOrderExample();

        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        example.setOrderByClause(LitemallVoucherOrder.Column.payTime.desc());
        PageHelper.startPage(page, size);
        return voucherOrderMapper.selectByExample(example);
    }

    public List<LitemallVoucherOrderVo> selectMchanismSimpleInfo(Integer userId, Integer showType, String name, Integer page, Integer size){
        PageHelper.startPage(page, size);
        return voucherOrderMapper.selectMchanismSimpleInfo(userId, showType, name);
    }

    public List<LitemallVoucherOrderVo> selectTotalMchanismSimpleInfo(Integer userId, Integer showType, String name){
        return voucherOrderMapper.selectMchanismSimpleInfo(userId, showType, name);
    }

    public int selectMchanismSimpleInfoCount(Integer userId, Integer showType, String name) {
        return voucherOrderMapper.selectMchanismSimpleInfo(userId, showType, name).size();
    }

    public LitemallVoucherOrder findById(Integer orderId) {
        return voucherOrderMapper.selectByPrimaryKey(orderId);
    }

    public int update(LitemallVoucherOrder voucherOrder) {
        return voucherOrderMapper.updateByPrimaryKeySelective(voucherOrder);
    }

    public void add(LitemallVoucherOrder voucherOrder){
        voucherOrderMapper.insertSelective(voucherOrder);
    }
    public int  updateStateAndNumberByOrderSn(String voucherOrderSn){
        return  voucherOrderMapper.updateByVoucherOrderSn(voucherOrderSn);
    }
    public LitemallVoucherOrder  queryOrderInfoByOrderSn (String voucherOrderSn){
        LitemallVoucherOrderExample example = new LitemallVoucherOrderExample();
        example.or().andOrderSnEqualTo(voucherOrderSn);
        return voucherOrderMapper.selectOneByExample(example);
    }
    public List<LitemallVoucherOrder>  queryOrderListBymechanismId (Integer mechanismId,Integer page, Integer size){
        PageHelper.startPage(page, size);
        LitemallVoucherOrderExample example = new LitemallVoucherOrderExample();
        example.or().andMechanismIdEqualTo(mechanismId);
        return voucherOrderMapper.selectByExample(example);
    }

    public int countSelectives(Integer mechanismId){
        LitemallVoucherOrderExample example = new LitemallVoucherOrderExample();
        example.or().andMechanismIdEqualTo(mechanismId);
        return (int)voucherOrderMapper.countByExample(example);
    }
}